<?php
/**
 * @author quangtran 
 *
 */

class Admin_ArticleController extends Admin_Libraries_Controller_Action 
{
	private $_params = null;
	protected $_config = null;
	
	public function init() 
	{
		parent::init();
		$this->_config = Zend_Registry::get('configuration');			
		$this->_params = $this->getRequest()->getParams();		
	}
		
	public function indexAction()
	{	
		if (!$this->_acl->isUserAllowed('admin:article', 'view'))
			$this->_redirect($this->_config->site->admin->urlAccessDenied); 
			
		$this->view->headScript()->appendFile( $this->_config->site->admin->jsUrl.'jform.js', 'text/javascript');		
		$arrWhere = array();
		$filterActive = $this->getRequest()->getParam('filter_active', '*');
		$filterKeyword = $this->getRequest()->getParam('filter_keyword', '');
		$filterCategory = $this->getRequest()->getParam('filter_category', '');
		if ($filterKeyword != '') $arrWhere[] = 'title like "%'.$filterKeyword.'%"';
		if ($filterCategory != '') $arrWhere[] = 'cat_id='.$filterCategory;
		if ($filterActive != '*') $arrWhere[] = 'active='.$filterActive;	

		$offset = $this->getRequest()->getParam('offset', 0);
		$limit = $this->getRequest()->getParam('limit', 0);
		if ($offset == 0) $offset = $this->_config->site->paginator->offset;
		
		$cArticle = new Admin_Model_Article();
		$cCategory = new Admin_Model_Category();
		$cUser = new Admin_Model_User();		
		$utilArray = new Core_Utilities_Array();
		$listArticle = $cArticle->getList($arrWhere, 'created_time DESC', $offset, $limit);	
		
		$this->view->arrUser = $utilArray->buildArrayWithCustomKey($cUser->getListManager(), 'id');
		$this->view->arrCat = $utilArray->buildArrayWithCustomKey($cCategory->getList(), 'id');
		$this->view->arrArticle = $listArticle['result'];					
		$this->view->filterActive = $filterActive;
		$this->view->filterKeyword = $filterKeyword;
		$this->view->filterCategory = $filterCategory;
		$this->view->message = $this->showMessage($this->_helper->FlashMessenger->getMessages());		
		unset($cArticle, $cArticle, $cUser);
	}
	
	public function addAction()
	{
		if (!$this->_acl->isUserAllowed('admin:article', 'add'))
			$this->_redirect($this->_config->site->admin->urlAccessDenied);
		
		$this->view->headLink ()->appendStylesheet ( $this->_config->site->jsUrl.'fancybox/fancybox.css' );
		$this->view->headScript()->appendFile( $this->_config->site->admin->jsUrl.'jform.js', 'text/javascript');		
		$this->view->headScript()->appendFile( $this->_config->site->jsUrl.'fancybox/mousewheel.pack.js', 'text/javascript');
		$this->view->headScript()->appendFile( $this->_config->site->jsUrl.'fancybox/fancybox.js', 'text/javascript');
		$this->view->headScript()->appendFile( $this->_config->site->jsUrl . 'ckeditor/ckeditor.js', 'text/javascript');
		$this->view->headScript()->appendScript("CKEDITOR.replace( 'editor',{ skin : 'kama'});");
		$flashMessage = $this->_helper->getHelper('FlashMessenger');
		$cCategory = new Admin_Model_Category();
		if ($this->getRequest()->isPost()) {
			$post = $this->getRequest()->getPost();			
			$notEmpty = new Zend_Validate_NotEmpty();
			$isValid = TRUE;
			if (!$notEmpty->isValid($post['jform']['title'])) $isValid = FALSE;
			if (!$notEmpty->isValid($post['jform']['alias'])) $isValid = FALSE;						 
			if ($isValid) {
				$data = array();
				$data['title'] = $post['jform']['title'];
				$data['alias'] = $post['jform']['alias'];
				$data['parent_id'] = $post['jform']['parent_id'];
				$data['description'] = stripslashes($post['jform']['description']);
				$data['active'] = $post['jform']['published'];
				$response = $cCategory->insert($data, $this->_auth->id);		
				if ($response > 0) {
					$flashMessage->addMessage(array('message'=> $this->view->translate('admin_save_successfull')));
					if (isset($post['save']) && $post['save'] == 'save') $this->_redirect('/admin/category');
				} else $flashMessage->addMessage(array('error' => $this->view->translate('admin_save_fail')));				
			}
		}	
					
		$arrCat = $cCategory->getList();
		$utilArray = new Core_Utilities_Array();		
		$this->view->arrCat = $utilArray->buildLevelArray($arrCat);	
		$this->view->message = $this->showMessage($flashMessage->getMessages());
	}
	
	public function editAction()
	{
		if (!$this->_acl->isUserAllowed('admin:article', 'edit'))
			$this->_redirect($this->_config->site->admin->urlAccessDenied);		
		
		$this->view->headLink ()->appendStylesheet ( $this->_config->site->jsUrl.'fancybox/fancybox.css' );
		$this->view->headScript()->appendFile( $this->_config->site->admin->jsUrl.'jform.js', 'text/javascript');		
		$this->view->headScript()->appendFile( $this->_config->site->jsUrl.'fancybox/mousewheel.pack.js', 'text/javascript');
		$this->view->headScript()->appendFile( $this->_config->site->jsUrl.'fancybox/fancybox.js', 'text/javascript');
		$this->view->headScript()->appendFile( $this->_config->site->jsUrl.'ckeditor/ckeditor.js', 'text/javascript');
		$this->view->headScript()->appendScript("CKEDITOR.replace( 'editor',{ skin : 'kama'});");
		$flashMessage = $this->_helper->getHelper('FlashMessenger');
		
		$id = $this->getRequest()->getParam('id', 0);
		if ($id <= 0) {
			$flashMessage->addMessage(array('notice'=>$this->view->translate('admin_id_invalid')));
			$this->_redirect('/admin/category');
		}
				
		$cCategory = new Admin_Model_Category();
		if ($this->getRequest()->isPost()) {
			$post = $this->getRequest()->getPost();
			$notEmpty = new Zend_Validate_NotEmpty();
			$isValid = TRUE;
			if (!$notEmpty->isValid($post['jform']['title'])) $isValid = FALSE;
			if (!$notEmpty->isValid($post['jform']['alias'])) $isValid = FALSE;				
			if ($isValid) {
				$data = array();
				$data['id'] = $post['jform']['id'];
				$data['title'] = $post['jform']['title'];
				$data['alias'] = $post['jform']['alias'];
				$data['parent_id'] = $post['jform']['parent_id'];
				$data['description'] = stripslashes($post['jform']['description']);
				$data['active'] = $post['jform']['published'];				
				$response = $cCategory->update($data, $this->_auth->id);		
				if ($response > 0) {
					$flashMessage->addMessage(array('message' => $this->view->translate('admin_save_successfull')));
					$this->_redirect('/admin/category');
				} else $flashMessage->addMessage(array('error'=> $this->view->translate('admin_save_fail')));				
			} else $flashMessage->addMessage(array('error'=> $this->view->translate('admin_form_data_notvalid')));
		}	
						
		$detail = $cCategory->getDetail($id);
		if (empty($detail)) {
			$flashMessage->addMessage(array('notice'=>$this->view->translate('admin_object_notexist')));
			$this->_redirect('/admin/category');
		}
				
		$arrCat = $cCategory->getList();		
		$utilArray = new Core_Utilities_Array();
		$this->view->arrCat = $utilArray->buildLevelArray($arrCat);
		$this->view->message = $this->showMessage($flashMessage->getMessages());	
		$this->view->detail = $detail;				
	}
	
	public function deleteAction()
	{
		if (!$this->_acl->isUserAllowed('admin:article', 'delete'))
			$this->_redirect($this->_config->site->admin->urlAccessDenied);
		$flashMessage = $this->_helper->getHelper('FlashMessenger');
		$id = $this->getRequest()->getParam('id', 0);
		if ($id <= 0) {
			$ids = $this->getRequest()->getParam('ids', null);
			if (is_null($ids)) {
				$flashMessage->addMessage(array('notice'=>$this->view->translate('admin_id_invalid')));
				$this->_redirect('/admin/category');
			}
			else $ids = implode(',', $ids);
		} else $ids = $id;
		
		$cCategory = new Admin_Model_Category();
		$response = $cCategory->delete($ids);
		if ($response > 0) $flashMessage->addMessage(array('message' => $this->view->translate('admin_form_delete_success')));
		else $flashMessage->addMessage(array('error' => $this->view->translate('admin_form_delete_fail')));
		$this->_redirect('/admin/category');
	}	
	
	public function publishAction()
	{
		if (!$this->_acl->isUserAllowed('admin:article', 'edit'))
			$this->_redirect($this->_config->site->admin->urlAccessDenied);		
		$id = $this->getRequest()->getParam('id', 0);
		if ($id <= 0) {
			$ids = $this->getRequest()->getParam('ids', null);
			if (is_null($ids)){
				$flashMessage->addMessage(array('notice'=>$this->view->translate('admin_id_invalid')));
				$this->_redirect('/admin/category');
			} else $ids = implode(',', $ids);
		} else $ids = $id;
		
		$cCategory = new Admin_Model_Category();
		$ultilArray = new Core_Utilities_Array();
		$arrCatId = $ultilArray->findChild($cCategory->getList(), $id);
		if (sizeof($arrCatId)) $response = $cCategory->updateStatus(array('active'=>1), implode(',', $arrCatId));	
		$this->_redirect('/admin/category');
	}
	
	public function unpublishAction()
	{
		if (!$this->_acl->isUserAllowed('admin:article', 'edit'))
			$this->_redirect($this->_config->site->admin->urlAccessDenied);	
		$id = $this->getRequest()->getParam('id', 0);
		if ($id <= 0) {
			$ids = $this->getRequest()->getParam('ids', null);
			if (is_null($ids)) {
				$flashMessage->addMessage(array('notice'=>$this->view->translate('admin_id_invalid')));
				$this->_redirect('/admin/category');
			} else $ids = implode(',', $ids);
		} else $ids = $id;
		
		$cCategory = new Admin_Model_Category();
		$ultilArray = new Core_Utilities_Array();
		$arrCatId = $ultilArray->findChild($cCategory->getList(), $id);		
		if (sizeof($arrCatId)) $response = $cCategory->updateStatus(array('active'=>0), implode(',', $arrCatId));		
		$this->_redirect('/admin/category');
	}
		
}